package Controller.transactional;

import Models.dao.DetailDao;
import Models.dao.TransactionalDao;
import Models.dao.TypeDao;
import Models.enity.Detail;
import Models.enity.Transactional;
import Models.enity.Type;
import com.sun.org.apache.regexp.internal.RE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/transactional/NeweventHandle")
public class NeweventHandle extends HttpServlet {
    private static String PublicPath = "/WEB-INF/views";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        TypeDao typeDao = new TypeDao();
        List<Type> TypeData = typeDao.findAll();


        DetailDao detailDao = new DetailDao();
        List<Detail> DetailData = detailDao.findAll();
        req.setAttribute("TypeData",TypeData);
        req.setAttribute("DetailData",DetailData);
        req.getRequestDispatcher(PublicPath + "/transactional/NeweventHandle.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Transactional transactional = new Transactional();
        transactional.setType_id(req.getParameter("type_id"));
        transactional.setDetail_id(req.getParameter("detail"));
        transactional.setInsurance_level(req.getParameter("insurance_level"));
        transactional.setIsFirstCommit(req.getParameter("isFirstCommit"));
        transactional.setHandleDepartment(req.getParameter("HandleDepartment"));
        transactional.setResponseTime(req.getParameter("ResponseTime"));
        transactional.setSolveTime(req.getParameter("SolveTime"));
        transactional.setNote(req.getParameter("note"));
        transactional.setDescription(req.getParameter("description"));
        transactional.setApplyPlace(req.getParameter("applyPlace"));
        transactional.setApplyPerson(req.getParameter("applyPerson"));
        transactional.setTelephone(req.getParameter("telephone"));
        transactional.setReportPlace(req.getParameter("reportPlace"));
        transactional.setStatus_id("1");
        transactional.setLevel_id("1");
        transactional.setCuiban_count("0");

        Date date = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd hh:mm:ss");
        String created_at = ft.format(date);
        transactional.setCreated_at(created_at);

        TransactionalDao transactionalDao = new TransactionalDao();
        int tmp = transactionalDao.insert(transactional);
        if (tmp != -1){
            resp.sendRedirect("/transactional/eventHandle");
        }
    }
}
